/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.IPlay;
import FrameWork.IState;
import FrameWork.Level;
import FrameWork.RSFont;
import FrameWork.ReadMap;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author DANTRUONG
 */
public class State_Loading extends IState{
    Sprite m_Ball1;
    Sprite m_Ball;
    int m_IDChoosed;
    ReadMap m_ReadMap;
    float m_TimeUpdate;
    int m_Status;
    
    public State_Loading(IPlay _Play){
        super(_Play);
        m_IDState = Global.STATE_LOADING;
    }
    
    public void Init (){
        super.Init();
        try {
            m_Ball1 = new Sprite (Image.createImage("/Image/ball1.png"),
                    30, 30);
            m_Ball = new Sprite (Image.createImage("/Image/bubble.png"),
                    90, 90);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        m_IDChoosed = 0;
        m_ReadMap = new ReadMap();
        m_ReadMap.Read("/" + Level.get().getCurLevel());
//        m_ReadMap.Read("/demo");
        m_TimeUpdate = 0;
        m_Status =0;
    }
    
    public void Update (float _Time){
        if(m_Status == 0){
            m_TimeUpdate += _Time;
            if(m_TimeUpdate >= 0.8f){
                m_TimeUpdate = 0;
                m_IDChoosed += 1;
            }
            if(m_IDChoosed == 2){
                m_TimeUpdate = 0;
                m_Status = 1;
            }
        }
        if(m_Status == 1){
            m_TimeUpdate += _Time;
            if(m_TimeUpdate >= 0.4f){
                m_TimeUpdate = 0;
                m_IDChoosed += 1;
            }
            if(m_IDChoosed == 6)
                m_Status = 2;
        }
        if(m_Status == 2){
            m_TimeUpdate += _Time;
            if(m_TimeUpdate >= 1.5f){
                m_TimeUpdate = 0;
                m_Play.setNextState(new State_MainGame(m_Play,m_ReadMap));
            }
        }
    }

    public void Render (){
        super.Render();
        m_Graphics.setColor(0, 0, 0);
        m_Graphics.fillRect(0, 0, 240, 320);
        if(m_Status == 2){
            if(Level.get().getCurLevel()<10)
                RSFont.getRS().getFont(Global.FONT_BIG).DrawString(
                                "Level " + Level.get().getCurLevel(), 88 , 130 + 21, m_Graphics);
            else
                RSFont.getRS().getFont(Global.FONT_BIG).DrawString(
                                "Level " + Level.get().getCurLevel(), 84 , 130 + 21, m_Graphics);
            m_Ball.setFrame(0);
            m_Ball.setPosition(75, 115);
            m_Ball.paint(m_Graphics);
        }else{
            RSFont.getRS().getFont(Global.FONT_BIG).DrawString(
                                "Loading...", 90, 160, m_Graphics);
            for(int i = 1; i<= m_IDChoosed; i++){
                m_Ball1.setFrame(1);
                m_Ball1.setPosition(10 + (i-1)*38, 180);
                m_Ball1.paint(m_Graphics);
            }
            for(int i = m_IDChoosed + 1; i<= 6; i++){
                m_Ball1.setFrame(0);
                m_Ball1.setPosition(10 + (i-1)*38, 180);
                m_Ball1.paint(m_Graphics);
            }
        }
    }
    
    public void Destroy (){
        m_Ball1 = null;
        m_Ball = null;
    }
}
